﻿using System.Collections.Generic;
using System.Web.Mvc;
using BrnMall.Core;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Manager.Domain.User;
using BrnMall.Manager.Manager.User;
using System.Text;
using System;
using System.Data;
using BrnMall.Web.StoreAdmin.Models;

namespace BrnMall.Web.StoreAdmin.controllers
{
    public partial class CashOrderController : BaseStoreAdminController
    {
        public ActionResult List(string accountName = "", string osn = "", int orderState = 0, string startTime = "", string endTime = "", int pageNumber = 1, int pageSize = 15)
        {
            accountName = accountName.Trim();
            osn = osn.Trim();
            startTime = startTime.Trim();
            endTime = endTime.Trim();
            List<SelectListItem> orderStateList = new List<SelectListItem>();
            orderStateList.Add(new SelectListItem() { Text = "全部", Value = "0" });
            orderStateList.Add(new SelectListItem() { Text = "等待付款", Value = ((int)OrderState.WaitPaying).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "待确认", Value = ((int)OrderState.Confirming).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "已付款", Value = ((int)OrderState.Confirmed).ToString() });


            //获取列表
            var dbList = CashOrderManager.GetListPage(WorkContext.StoreId, FillterSql(accountName), FillterSql(osn), orderState, FillterSql(startTime), FillterSql(endTime), pageNumber, pageSize);

            //填充会员
            var list = FillUser(dbList.Items);

            var model = new Models.CashOrderListModel()
            {
                AccountName = accountName,
                OrderState = orderState,
                OSN = osn,
                StartTime = "",
                EndTime = "",
                List = list,
                OrderStateList = orderStateList,
                PageModel = new PageModel(pageSize, pageNumber, (int)dbList.TotalItems)
            };

            return View(model);
        }

        /// <summary>
        /// 过滤危险字符
        /// </summary>
        string FillterSql(string sql)
        {
            sql = sql.Replace("'", "");
            sql = sql.Replace("exec", "");
            sql = sql.Replace("delete", "");
            sql = sql.Replace("master", "");
            sql = sql.Replace("truncate", "");
            sql = sql.Replace("declare", "");
            sql = sql.Replace("create", "");
            sql = sql.Replace("xp_", "no");
            return sql;
        }
        List<CashOrderView> FillUser(List<CashOrder> list)
        {
            //会员ID
            StringBuilder sbId = new StringBuilder();
            foreach (var item in list)
            {
                sbId.AppendFormat(",{0}", item.Uid);
            }
            if (sbId.Length > 0)
                sbId.Remove(0, 1);

            //订单列表
            List<CashOrderView> listOrder = new List<CashOrderView>();
            //订单的会员列表
            var connection = string.Format("bma_users.uid in({0})", sbId.ToString());
            var userList = sbId.Length < 1 ? new DataTable() : Users.GetUserList(list.Count, 1, connection);

            foreach (var item in list)
            {
                CashOrderView order = new CashOrderView();
                order.id = item.id;
                order.Uid = item.Uid;
                order.CashItemId = item.CashItemId;
                order.CreateTime = item.CreateTime;
                order.GiveAmount = item.GiveAmount;
                order.IP = item.IP;
                order.OrderAmount = item.OrderAmount;
                order.OrderState = item.OrderState;
                order.OSN = item.OSN;
                order.paySN = item.paySN;
                order.payTime = item.payTime;
                order.prepay_id = item.prepay_id;
                order.StoreId = item.StoreId;

                foreach (DataRow user in userList.Rows)
                {
                    if (Convert.ToInt32(user["uid"]) == item.Uid)
                    {
                        order.AccountName = Convert.ToString(user["NickName"]);
                        break;
                    }
                }
                listOrder.Add(order);
            }
            return listOrder;
        }
    }
}